package com.feshfans.byteconvert.convertor;

import com.feshfans.byteconvert.annotation.Endian;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class LongConvertor extends AbstractConvertor<Long> {
    @Override
    public Long fromBytes(byte[] bytes) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(8);
        if(endianType == Endian.EndianType.Little){
            byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        } else {
            byteBuffer.order(ByteOrder.BIG_ENDIAN);
        }
        byteBuffer.put(bytes,0,bytes.length);
        byteBuffer.flip();
        return byteBuffer.getLong();
    }

    @Override
    public byte[] toBytes(Long aLong) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(8);
        if(endianType == Endian.EndianType.Little){
            byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        } else {
            byteBuffer.order(ByteOrder.BIG_ENDIAN);
        }

        byteBuffer.putLong(aLong);
        return byteBuffer.array();
    }
}
